using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Emlyn.Website.Users;
using System.Net.Mail;
using Emlyn.Common.Data.DBUpdater;
using Emlyn.Common.Data.Configuration;

public partial class RegisterUser : EmlynPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DBUpdaterMain.WebsiteCode = "EmlynORegan";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtPassword.Text != txtPassword2.Text)
                lblError.Text = "Passwords do not match";
            else if (String.IsNullOrEmpty(txtPassword.Text))
                lblError.Text = "Please provide a password";
            else if (String.IsNullOrEmpty(txtUsername.Text))
                lblError.Text = "Please provide a username";
            else if (String.IsNullOrEmpty(txtEmail.Text))
                lblError.Text = "Please provide an email address";
            else
            {
                User lcheckUser = (new UserQuery()).GetUserByUserName(txtUsername.Text);
                if (lcheckUser != null)
                {
                    lblError.Text = "User name is already taken. Please choose another.";
                }
                else
                {
                    EWebsite lwebsite = new EWebsite(DBUpdaterMain.WebsiteCode);

                    MailAddress lemail = new MailAddress(txtEmail.Text);
                    // throw an exception if the email address is broken

                    User luser = new User();

                    luser.UserName = txtUsername.Text;
                    luser.SetPassword(txtPassword.Text);
                    luser.Email = txtEmail.Text;
                    luser.AuthLevel = GlobalAuthLevel.WebsiteUser;
                    luser.WebsiteCode = DBUpdaterMain.WebsiteCode;

                    (new UserMod()).Save(luser);

//                    (new EWebsiteMod()).AddUserWebsiteRelationship(luser, lwebsite);

                    System.Net.Mail.SmtpClient lclient = new SmtpClient("smtp.gmail.com", 587);

                    MailMessage lmessage = new MailMessage(
                        "emlynoregan@gmail.com", txtEmail.Text, "Validate Registration with emlynoregan.com",
                        "Hey, thanks for registering! You need to validate your registration, which is easy! Just " +
                        "go here: http://emlynoregan.com/ValidateUser.aspx?user=" + luser.UserName + "&code=" + luser.ValidationCode + " ." + Environment.NewLine +
                        "---" + Environment.NewLine +
                        "Emlyn" + Environment.NewLine +
                        Environment.NewLine +
                        "emlynoregan.com"
                    );

                    lclient.Credentials = new System.Net.NetworkCredential("emlynoregan@gmail.com", Config.GetEmailPass());
                    lclient.EnableSsl = true;
                    lclient.Send(lmessage);

                    FormsAuthentication.RedirectFromLoginPage(luser.UserName, false);
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.GetType().Name + ": " + ex.Message;
        }
    }
}
